package melwi;

import java.awt.*;
import java.awt.event.*;


class Cteni extends java.awt.Frame implements java.awt.event.WindowListener, java.awt.event.ActionListener {

		
	public final static String tones[] = {"c", "cis", "d", "dis", "e", "f", "fis", "g", "gis", "a", "b", "h"};
	public final static String accord[] = {"dur", "moll", "dim", "7", "7maj", "9", "9-", "9+", "11", "5-", "5+", "4sus", "6sus", "add 9-", "add 9", "add9+", "add11", "/"};
	public final static int accord_num = accord.length; //kolik mame typu akordu

	private int tonesNum=tones.length;
	private boolean[] tonesUsed = new boolean[tonesNum];
	private boolean[] accordUsed = new boolean[accord_num];
	
	private int tonic = -1; //tonika
	private int bass = -1; //zmenena basovka
		

	String w_name = "Jmenem";
	String w_tones = "Tony";
	String w_end = "Hotovo";
	String eMollDur = "Chybne zadani, akord nemuze byt typu moll i dur zaroven";	
	String eKvinta = "Chybne zadani, akord nemuze byt zaroven 5+, 5- nebo 6sus";
	String eToneNum = "Zadano vice tonu, nez ma nastroj strun";	
		
	private TextField f1;
		
		//int strings[] = {5, 10, 3, 8, 0, 5}
		//promyslet cislovani, tonu, jestli od nuly, a tak...
	//struny cislovany opacne !!!!!!!!!!!!!!!
	//pocitat, ocernovat uz vykonane
	Button cudlik;			
public Cteni(){

	super("Generator akordu");
	this.setSize(350,200);
	this.setLayout(new FlowLayout());	//nastavi layout tohodle wokna na nove vytvoreny FlowLayout
	this.addWindowListener(this);	//nastavi se kdyz se bude neco dit s oknem vyresi to methoda tohodle objektu
        
	for(int i=0;i<tonesNum;i++){
         	cudlik = new Button(tones[i]);
		cudlik.setActionCommand(tones[i]);
                cudlik.addActionListener(this);
                this.add(cudlik);
        }                                                            
	for(int i=0;i<accord_num;i++){
	        cudlik = new Button(accord[i]);
                cudlik.setActionCommand(accord[i]);
                cudlik.addActionListener(this);
                this.add(cudlik);                               
	}
                
	f1 = new TextField(20);
	this.add(f1);

        cudlik = new Button(w_end); 
        cudlik.setActionCommand(w_end);
	cudlik.addActionListener(this);	
        this.add(cudlik);
		
	this.show();
	
}
		public void windowDeactivated(java.awt.event.WindowEvent p1) {
		}
		
		public void windowClosed(java.awt.event.WindowEvent p1) {
			System.exit(0);
		}
		
		public void windowDeiconified(java.awt.event.WindowEvent p1) {
		}
		
		public void windowOpened(java.awt.event.WindowEvent p1) {
		}
		
		public void windowIconified(java.awt.event.WindowEvent p1) {
		}
		
		public void windowClosing(java.awt.event.WindowEvent p1) {
			this.dispose();
		}
		
		public void windowActivated(java.awt.event.WindowEvent p1) {
		}
		
		public void actionPerformed(java.awt.event.ActionEvent p1) {
                
        		for(int i=0;i<tonesNum;i++){
				if(p1.getActionCommand().equals(tones[i])){                       
                       			//System.out.println(i);
					f1.setText(f1.getText()+" "+tones[i]);
					if (tonic==-1){
						tonic=i; //zada toniku
					}
					if(bass==-1 && accordUsed[accord_num-1]){  //zada basovku
						bass=i;
					}
					else{
						tonesUsed[i]=true;
					}
				}
			}
			
			
        		for(int i=0;i<(accord_num);i++){
				if(p1.getActionCommand().equals(accord[i])){                       
                       			//System.out.println(i);
					f1.setText(f1.getText()+" "+accord[i]);
				
					accordUsed[i]=true;
				}
			}

			if(p1.getActionCommand().equals(w_end)){                       
				this.setVisible(false);
				zpracujTony();
			 
				int[] poleA = this.getField();
				if(poleA.length>Nastroj.stringNum){
					System.out.println(eToneNum);
				}
				else{
					if(bass==-1){
						System.out.println("poustim generator");
						Generator aktgenerator = new Generator(poleA); //volame generator
					}
					else{
						Generator aktgenerator = new Generator(poleA, bass);
					}
				}
			}
                }
		private void zpracujTony(){
			if (! accordUsed[1] && ! accordUsed[2]) //nastaveni akordu na dur
				accordUsed[0]=true;
		
		 
			if ((accordUsed[0] && accordUsed[1]) || (accordUsed[0] && accordUsed[2])) //dur a zaroven moll
		 		System.out.println(eMollDur);
			if ((accordUsed[9] && accordUsed[10]) || (accordUsed[9] && accordUsed[12])||(accordUsed[10] && accordUsed[12])) //kvinta + / - a zaroven 6 sus
		 		System.out.println(eKvinta);

			if (accordUsed[0])
				dur();
			
			if (accordUsed[1])
				moll();
			
			if (accordUsed[2])
				dim();
			if (accordUsed[3])
				septima();
			if (accordUsed[4])
				maj();
			if (accordUsed[5]){ //9
				septima();
				nona();
			}
			if (accordUsed[6]){ //9-
				septima();
				nonaMin();
			}
			if (accordUsed[7]){ //9+
				septima();
				nonaMaj();
			}
			if (accordUsed[8]){ //11
				septima();
				nona();
				undecima();
			}
			if (accordUsed[9]){ //5-
				kvintaMin();
			}
			if (accordUsed[10]){ //5+
				kvintaMaj();
			}
			if (accordUsed[11]){ //4sus
				kvarta();
			}
			if (accordUsed[12]){ //6sus
				sexta();
			}
			if (accordUsed[13]){ //add9-
				nonaMin();
			}
			if (accordUsed[14]){ //add9
				nona();
			}
			if (accordUsed[15]) //add9+
				nonaMaj();
			if (accordUsed[16]) //aadd11
				undecima();
       		
			for(int i=0;i<tonesNum;i++){ //kontrola
				 System.out.println(tonesUsed[i]);
			 }

			
			 

		}
			public void dur(){
				tonesUsed[(tonic+4)%tonesNum]=true;
				tonesUsed[(tonic+7)%tonesNum]=true;
			}
			public void moll(){
				tonesUsed[(tonic+3)%tonesNum]=true;
				tonesUsed[(tonic+7)%tonesNum]=true;
			}
			public void dim(){
				tonesUsed[(tonic+3)%tonesNum]=true;
				tonesUsed[(tonic+6)%tonesNum]=true;
				tonesUsed[(tonic+9)%tonesNum]=true;
			}
			public void septima(){
				tonesUsed[(tonic+10)%tonesNum]=true;
			}
			public void maj(){
				tonesUsed[(tonic+11)%tonesNum]=true;
			}
			public void nonaMin(){
				tonesUsed[(tonic+13)%tonesNum]=true;
			}
			public void nona(){
				tonesUsed[(tonic+14)%tonesNum]=true;
			}
			public void nonaMaj(){
				tonesUsed[(tonic+15)%tonesNum]=true;
			}
			public void undecima(){
				tonesUsed[(tonic+17)%tonesNum]=true;
			}
			public void kvintaMin(){
				tonesUsed[(tonic+6)%tonesNum]=true;
				tonesUsed[(tonic+7)%tonesNum]=false;
			} 
			public void kvintaMaj(){
				tonesUsed[(tonic+8)%tonesNum]=true;
				tonesUsed[(tonic+7)%tonesNum]=false;
			}
			public void kvarta(){
				tonesUsed[(tonic+5)%tonesNum]=true;
				tonesUsed[(tonic+4)%tonesNum]=false;
			}
			public void sexta(){
				tonesUsed[(tonic+9)%tonesNum]=true;
				tonesUsed[(tonic+7)%tonesNum]=false;
			}
/*prevede pole dvanacti booleanu na pole delky uzitych tonu a jejich hodnot*/
	private int[] fieldTransform(boolean[] dvanactka){ 
		int citadlo=0;
		int pomoc=0;
		for (int i=0; i<tonesNum; i++){
			if (dvanactka[i]){
				citadlo++;
			}
		}
		int[] toneList=new int[citadlo];
		for (int i=0; i<tonesNum; i++){
			if (dvanactka[i]){
				toneList[pomoc]=i;
				pomoc++;
			}
		}
		return toneList;
	}

	public int[] getField(){
	
		return fieldTransform (tonesUsed);
	}

			//ujistit se, ze basovka je stejna, poresit toi!!!!!!!!!!!!!!!!!!!!
			//zkontrolovat, ze se nic neprepisuje
			//dodelat testy na kolize
			//System.out.println("zprac");
		

}
